package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.AccidentCase
import cn.exrick.xboot.base.entity.Area
import cn.exrick.xboot.base.entity.LawAndRegulations
import cn.exrick.xboot.base.entity.SupervisionUser
import cn.exrick.xboot.base.mapper.AccidentCaseMapper
import cn.exrick.xboot.base.mapper.AreaMapper
import cn.exrick.xboot.base.mapper.LawAndRegulationsMapper
import cn.exrick.xboot.base.service.*
import cn.exrick.xboot.base.vo.AccidentCaseVo
import cn.exrick.xboot.base.vo.LawAndRegulationsVo
import cn.exrick.xboot.core.common.constant.CommonConstant
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.mybatis.IUserRoleService
import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import reactor.core.publisher.Flux
import reactor.core.scheduler.Schedulers
import java.util.*
import javax.annotation.Resource

/**
 * 法律法规接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ILawAndRegulationsServiceImpl : ServiceImpl<LawAndRegulationsMapper?, LawAndRegulations?>(), ILawAndRegulationsService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    override fun createOrUpdateLawAndRegulations(lawAndRegulations: LawAndRegulations): Result<Any?>? {

        return try {

            lawAndRegulations.areaId = securityUtil.currUser.areaId
            saveOrUpdate(lawAndRegulations)
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeLawAndRegulations(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("移除成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getLawAndRegulationsList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(LawAndRegulations::class.java).eq(LawAndRegulations::areaId, securityUtil.currUser.areaId)
                .orderByDesc(LawAndRegulations::createTime)))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { LawAndRegulationsVo().apply { ToolUtil.copyProperties(it, this) } }.toMutableList())

            ResultUtil.data(result, "获取数据成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun getLawAndRegulationDetail(id: String?): Result<LawAndRegulations?>? {

        return ResultUtil.data(getById(id))
    }
}